class Department < ActiveRecord::Base
  has_many :users
  has_many :scores


  scope :count_by_user, ->(dep) do
    if dep.level == 1
      group(:master_id)
    elsif dep.level == 2
      where("departments.master_id = ? or departments.id = ?", dep.id, dep.id)
        .group('departments.id')
    elsif dep.level == 3
      where("departments.id = ?", dep.id)
        .group(:id_type)
    end
  end
end
